package com.pan.service;


import com.pan.entity.Animal;
import com.pan.repository.AnimalRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AnimalService {

    @Autowired
    private AnimalRepository animalRepository;

    public Animal findById(String animalId){
        Optional<Animal> byId = animalRepository.findById(animalId);
        return byId.get();
    }

    public List<Animal> findAll(){
        List<Animal> all = animalRepository.findAll();
        return all;
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Animal save(Animal animal){
        Animal animalSave = animalRepository.save(animal);
        return animalSave;
    }
    public void update(Animal animal){
        animalRepository.save(animal);
    }
    public void delete(String id){
        animalRepository.deleteById(id);

    }

}
